package com.dean.test.csv;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 批量修改文件中内容
 */
public class BatchChangeTextContent {
    Logger logger = LoggerFactory.getLogger(BatchChangeTextContent.class);

    /**
     * 批量替换 文件中的内容
     * @param files
     * @param replaceRule
     */
    public void batchReplaceFileChar(File[] files,Map<String,String> replaceRule){
        for (File file : files) {
            replaceFileChar(file,replaceRule);
        }
    }

    /**
     * 替换文件中的字符按串
     * @param file
     * @param replaceRule
     */
    public void replaceFileChar(File file, Map<String,String> replaceRule){
        // 看看文件有多大
        Long fileLength = file.length();
        byte [] fileContext = new byte[fileLength.intValue()];
        // 将替换后的内容重新写入文件 在当前目录创建新的目录用于存放替换后的文件
        File newDir = new File(file.getParent() + File.separator + "new");
        if(!newDir.exists()){
            newDir.mkdirs();
        }
        String newFileName = newDir+File.separator+file.getName();
        try (FileInputStream fis = new FileInputStream(file);
             FileWriter writer = new FileWriter(newFileName);)
        {
            // 将内容读取到字节流数组中
            fis.read(fileContext);
            // 将字节流数组转为字符串
            String content = new String(fileContext,"utf-8");
            // 根据替换规则替换字符串
            Set<String> keys = replaceRule.keySet();
            for (String key : keys) {
                content = content.replace(key,replaceRule.get(key));
            }
            // 将内容写入文件中
            writer.write(content);
            logger.debug("文件[{}]已完成字符替换,替换后的文件存于[{}]",file.getName(),newFileName);
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage(),e);
        } catch (IOException e) {
            logger.error(e.getMessage(),e);
        }
    }

    public static void main(String[] args) throws IOException {
        BatchChangeTextContent changeTextContent = new BatchChangeTextContent();
        // changeTextContent.replaceFileChar(new File("F:\\dbs\\kettle\\kettle\\dbs_ods_imex_doc_src.ktr"),new HashMap<>());
        // kettle 文件目录
        File filePath = new File("\\F:\\dbs\\kettle\\kettle");
        File[] files = filePath.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if(name.endsWith(".ktr")){
                    return true;
                }
                return false;
            }
        });
        // 创建替换规则
        Map<String,String> rule = new HashMap<String,String>();
        rule.put("<commit>1000</commit>","<commit>10000</commit>");
        rule.put("data_date = '${data_date}'","data_date &lt;= '${end_data_date}' and data_date &gt;= '${start_data_date}'");
        rule.put("    <parameters>\n" +
                "      <parameter>\n" +
                "        <name>data_date</name>\n" +
                "        <default_value/>\n" +
                "        <description/>\n" +
                "      </parameter>\n" +
                "    </parameters>","<parameters>\n" +
                "      <parameter>\n" +
                "        <name>end_data_date</name>\n" +
                "        <default_value>20220531</default_value>\n" +
                "        <description />\n" +
                "      </parameter>\n" +
                "      <parameter>\n" +
                "        <name>start_data_date</name>\n" +
                "        <default_value>20220501</default_value>\n" +
                "        <description />\n" +
                "      </parameter>\n" +
                "    </parameters>");

        changeTextContent.batchReplaceFileChar(files,rule);

    }
}
